<?php
/**
 * User Groups and Authorization
 * 
 * Admin screen that display current user profile, authorizations, and
 * groups
 * 
 * @author Xavier VILAIN
 * @package Hive\User
 * @license #license-hive-social#
 */
use Fox\Behaviors;
use Fox\Generator;
use Orx\Kpi;
use Fox\Grid;
$xview = Xview::instance();
$id = $xview->application()->getId();
// Customizing
$customizing = Hive\User\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'users','act'=>'kpis','sac' => null));

// Page Title
$header = new Fox\Html\Header\Content('page-header',Msg(Hive::BUNDLE,Hive::SETUP_YOUR_KPIS));

// Load All KPI's
$kpis = Fox\Dao::finder(Kpi::className())
			->where('active = ? AND loekz = ?',array(1,0))
			->find();
$userPreferences = Fox\Dao::finder(Kpi\User\Preference::className())
					->where('idusr = ?',array(User::current()->getId()))
					->find();
$preferences = array();
foreach($userPreferences as $preference) {
	$preferences[$preference->kpiid] = $preference;
}
$html = null;
$html .= '<p class="prettyprint">'.Msg(Hive::BUNDLE,
			Hive::YOU_WILL_FIND_BELLOW_THE_LIST_OF_KPIS_AVAILABLE_ACCORDING_TO_YOUR_ACCOUNT_PLAN).'</p>';
// Load all available Kpi's
$grid = new Grid('kpis');
$grid->fieldcat(array(
					array('kpi_label','label'),
					array('kpi_description','description'),
					array('execute','followed'),
					array('execute','dashboard'),
				));
$grid->fieldcat()->switchToObjectMode();
$grid->fieldcat()
		->field('followed')
			->label(Msg(Orx\Kpi\Messages::BUNDLE,Orx\Kpi\Messages::FOLLOWED))
			->setGenerator(Generator::useFunction(function($data) use($preferences){
				if(isset($preferences[$data->kpiid] )) {
					$value = $preferences[$data->kpiid]->followed;
				} else {
					$value =  false;
				}
				$behavior = new Behaviors(Behaviors::action(
									Generator::url('users','kpis','save',array('kpiid' => $data->kpiid,'followed' =>true)),
									false,
									false,
									false,
									false,
									'POST',
									null,
									null,
									null,
									null,
									null,
									false,
									'contentCol'
								));
				$serialized = $behavior->serialize($data, 'kpi');
				if($value) {
					return '<a class="icons hastooltip '.$serialized.'" title="'.Msg(\Orx\Kpi\Messages::BUNDLE,\Orx\Kpi\Messages::CLICK_TO_DISABLE).'" >
								<i class="icon-ok">&nbsp;</i>
							</a>';
				} else {
					return '<a class="icons hastooltip '.$serialized.'" title="'.Msg(\Orx\Kpi\Messages::BUNDLE,\Orx\Kpi\Messages::CLICK_TO_ENABLE).'" >
								<i class="icon-remove">&nbsp;</i>
							</a>';
				}
				
			}));
$grid->fieldcat()
		->field('dashboard')
		->label(Msg(Orx\Kpi\Messages::BUNDLE,Orx\Kpi\Messages::IN_DASHBOARD))
		->setGenerator(Generator::useFunction(function($data) use($preferences){
				if(isset($preferences[$data->kpiid] )) {
					$value = $preferences[$data->kpiid]->dashboard;
				} else {
					$value =  false;
				}
				$behavior = new Behaviors(Behaviors::action(
									Generator::url('users','kpis','save',array('kpiid' => $data->kpiid,'dashboard' =>true)),
									false,
									false,
									false,
									false,
									'POST',
									null,
									null,
									null,
									true,
									null,
									false,
									'contentCol'
								));
				$serialized = $behavior->serialize($data, 'kpi');
				if($value) {
					return '<a class="icons hastooltip '.$serialized.'" title="'.Msg(\Orx\Kpi\Messages::BUNDLE,\Orx\Kpi\Messages::CLICK_TO_DISABLE).'" >
								<i class="icon-ok">&nbsp;</i>
							</a>';
				} else {
					return '<a class="icons hastooltip '.$serialized.'" title="'.Msg(\Orx\Kpi\Messages::BUNDLE,\Orx\Kpi\Messages::CLICK_TO_ENABLE).'" >
								<i class="icon-remove">&nbsp;</i>
							</a>';
				}
			}));
$grid->initialize($kpis);		
$html .= $grid->display();
?>
<div>
	<div class="page-header clearfix"><?php echo $header->render();?></div>
	<div class="contentArea source {'uri':'?mod=users&act=kpis&xvtrs=none'}">
		<div class="mbs ">
			<?php echo $html;?>
		</div>
	</div>
</div>